package com.example.androidlearning2200;

import android.os.Bundle;
import android.os.Environment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.example.androidlearning2200.http.HttpResponse;
import com.example.androidlearning2200.bean.UserInfo;
import com.example.androidlearning2200.http.HttpCallback;
import com.example.androidlearning2200.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        findViewById(R.id.btnGetBanner).setOnClickListener(view -> HttpUtil.doGet("https://www.wanandroid.com/banner/json", new HttpCallback() {
            @Override
            public void onHttpSucceed(String jsonStr) {
                Gson gson = new Gson();
                HttpResponse<List<LinkedTreeMap>> httpResponse = gson.fromJson(jsonStr, HttpResponse.class);
                List<LinkedTreeMap> bannerBeans = httpResponse.getData();
                System.out.println("第一个Banner轮播图地址: " + bannerBeans.get(1).get("imagePath"));
            }
        }));
        findViewById(R.id.btnPostRegister).setOnClickListener(view -> postRegister("https://www.wanandroid.com/user/register", "admin20240418", "admin123", "admin123"));
        findViewById(R.id.btnPostLogin).setOnClickListener(view -> postLogin("https://www.wanandroid.com/user/login", "admin20240418", "admin123"));
        findViewById(R.id.btnFileDownload).setOnClickListener(view -> {
            File ext = ContextCompat.getExternalFilesDirs(HttpActivity.this, Environment.DIRECTORY_DOWNLOADS)[0];
            File file = new File(ext, System.currentTimeMillis() + "jpg");
            HttpUtil.downloadFile("https://pic.rmb.bdstatic.com/bjh/events/2759255a159e57462e190276178cd8e73082.png@h_1280", file.getAbsolutePath());
        });
    }


    /**
     * Post方法 用户登录
     */
    private void postLogin(String urlString, String username, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        HttpUtil.doPost(urlString, params, new HttpCallback() {
            @Override
            public void onHttpSucceed(String jsonStr) {
                // 将json字符串解析成为JavaBean类
                Gson gson = new Gson();
                HttpResponse<UserInfo> httpResponse = gson.fromJson(jsonStr, HttpResponse.class);
                System.out.println("Nickname: " + httpResponse.getData().getNickname());
            }
        });
    }

    /**
     * Post方法 用户注册
     */
    private void postRegister(String urlString, String username, String password, String repassword) {
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("repassword", repassword);
        HttpUtil.doPost(urlString, params, new HttpCallback() {
            @Override
            public void onHttpSucceed(String jsonStr) {
                // 将json字符串解析成为JavaBean类
                Gson gson = new Gson();
                HttpResponse<UserInfo> httpResponse = gson.fromJson(jsonStr, HttpResponse.class);
                System.out.println("Nickname: " + httpResponse.getData().getNickname());
            }
        });
    }

}